package com.ysd.lis.service.qi.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.qi.*;
import com.ysd.lis.mapper.qi.*;
import com.ysd.lis.service.qi.QiIndexBusinessModuleService;
import com.ysd.lis.service.qi.QiIndexSqlBusinessService;
import com.ysd.lis.service.qi.QiQiIndexItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 指标报表字段表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-01-29
 */
@Service
public class QiQiIndexItemServiceImpl extends ServiceImpl<QiIndexItemMapper, QiIndexItem> implements QiQiIndexItemService {

    @Autowired
    QiIndexItemMapper qiIndexItemMapper;
    @Autowired
    QiIndexItemFilterMapper qiIndexItemFilterMapper;
    @Autowired
    QiIndexSqlExistsMapper qiIndexSqlExistsMapper;
    @Autowired
    QiIndexBusinessModuleService qiIndexBusinessModuleService;
    @Autowired
    private QiMajorBusinessMapper qiMajorBusinessMapper;
    @Autowired
    QiIndexSqlBusinessService qiIndexSqlBusinessService;

    @Override
    public Result getQiIndexItemList(String indexCode) {
        LambdaQueryWrapper<QiIndexItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QiIndexItem::getIndexCode, indexCode)
                .eq(QiIndexItem::getDelFlag, 0)
                .orderByAsc(QiIndexItem::getSeq);
        List<QiIndexItem> qiReportFileds = qiIndexItemMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", qiReportFileds);
    }

    @Override
    public Result getQiIndexItemListByMajorCode(String majorCode) {
        MPJLambdaWrapper<QiIndexItem> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(QiIndexItem.class);
        lambdaQueryWrapper.leftJoin(QiIndexMain.class, on -> on.eq(QiIndexItem::getIndexCode, QiIndexMain::getIndexCode).eq(QiIndexMain::getDelFlag, 0));
        lambdaQueryWrapper.leftJoin(QiIndexMajor.class, on -> on.eq(QiIndexMajor::getIndexCode, QiIndexMain::getIndexCode).eq(QiIndexMain::getDelFlag, 0));
        lambdaQueryWrapper.eq(QiIndexMajor::getMajorCode, majorCode);

        StringBuffer existsSql = new StringBuffer("SELECT 1 FROM QI_INDEX_ITEM_FILTER qiif WHERE t.ITEM_CODE = qiif.ITEM_CODE AND qiif.MAJOR_CODE = '" + majorCode + "' ");
        lambdaQueryWrapper.notExists(existsSql.toString());
        lambdaQueryWrapper.orderByAsc(QiIndexMain::getIndexCode);
        lambdaQueryWrapper.orderByAsc(QiIndexItem::getItemCode);
        List<QiIndexItem> qiIndexItems = qiIndexItemMapper.selectList(lambdaQueryWrapper);
        qiIndexItems.forEach(qiIndexItem -> {
            qiIndexItem.setMajorCode(majorCode);
        });
        return Result.succ(1, "查询成功", qiIndexItems);
    }

    @Override
    public Result getQiIndexItemFilterListByMajorCode(String majorCode) {
        MPJLambdaWrapper<QiIndexItemFilter> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(QiIndexItemFilter.class);
        lambdaQueryWrapper.leftJoin(QiIndexMain.class, on -> on.eq(QiIndexItemFilter::getIndexCode, QiIndexMain::getIndexCode).eq(QiIndexMain::getDelFlag, 0));
        lambdaQueryWrapper.eq(QiIndexMajor::getMajorCode, majorCode);
        lambdaQueryWrapper.orderByAsc(QiIndexMain::getIndexCode);
        lambdaQueryWrapper.orderByAsc(QiIndexItemFilter::getItemCode);
        List<QiIndexItemFilter> qiIndexItems = qiIndexItemFilterMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", qiIndexItems);
    }

    @Override
    public Result addQiIndexItem(QiIndexItem qiIndexItem) {
        boolean save = this.save(qiIndexItem);
        if (save) {
            return Result.succ(1, "添加成功", qiIndexItem);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result updateQiIndexItem(QiIndexItem qiIndexItem) {
        boolean save = this.updateById(qiIndexItem);
        if (save) {
            return Result.succ(1, "编辑成功", qiIndexItem);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result deleteQiIndexItem(QiIndexItem qiIndexItem) {
        boolean delete = this.removeById(qiIndexItem.getId());
        if (delete) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result addQiIndexItemFilter(QiIndexItemFilter qiIndexItemFilter) {
        int insert = qiIndexItemFilterMapper.insert(qiIndexItemFilter);
        if (insert > 0) {
            return Result.succ(1, "", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result deleteQiIndexItemFilter(QiIndexItemFilter qiIndexItemFilter) {
        int delete = qiIndexItemFilterMapper.deleteById(qiIndexItemFilter.getId());
        if (delete > 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }

    @Override
    public Result syncQiIndexItemData(List<QiIndexItem> qiIndexItems) {
        //根据专业code 查询出所有的对照信息
        LambdaQueryWrapper<QiIndexBusinessModule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(QiIndexBusinessModule::getDelFlag, 0);
        List<QiIndexBusinessModule> businessModules = qiIndexBusinessModuleService.list(lambdaQueryWrapper);
        //key是moduleType value是对应对照数据
        Map<String,List<QiMajorBusiness>> map=new HashMap<>();
        for (QiIndexBusinessModule businessModule : businessModules) {
            MPJLambdaWrapper<QiMajorBusiness> mpjLambdaWrapper = new MPJLambdaWrapper<>();
            mpjLambdaWrapper.selectAll(QiMajorBusiness.class)
                    .eq(QiMajorBusiness::getDelFlag, 0)
                    .eq(QiMajorBusiness::getMajorCode, qiIndexItems.get(0).getMajorCode())
                    .eq(QiMajorBusiness::getModuleType, businessModule.getModuleType())
                    .orderByAsc(QiMajorBusiness::getCreateTime);
            List<QiMajorBusiness> businesses = qiMajorBusinessMapper.selectList(mpjLambdaWrapper);
            map.put(businessModule.getModuleType(),businesses);
        }

        //查询项目的业务对照集合
        for (QiIndexItem qiIndexItem : qiIndexItems) {
            //Exists
            LambdaQueryWrapper<QiIndexSqlExists> queryExistsWrapper = new LambdaQueryWrapper<>();
            queryExistsWrapper
                    .eq(QiIndexSqlExists::getIndexCode,qiIndexItem.getIndexCode())
                    .eq(QiIndexSqlExists::getItemCode,qiIndexItem.getItemCode())
                    .eq(QiIndexSqlExists::getMajorCode,qiIndexItem.getMajorCode())
                    .eq(QiIndexSqlExists::getDelFlag,0);
            List<QiIndexSqlExists> qiIndexSqlExistss = qiIndexSqlExistsMapper.selectList(queryExistsWrapper);
            for (QiIndexSqlExists indexSqlExistss : qiIndexSqlExistss) {
                List<QiMajorBusiness> businesses = map.get(indexSqlExistss.getModuleType());
                if (CollectionUtils.isNotEmpty(businesses)){
                    //进行插入对照信息
                    for (QiMajorBusiness business : businesses) {
                        QiIndexSqlBusiness qiIndexSqlBusiness = new QiIndexSqlBusiness();
                        BeanUtil.copyProperties(qiIndexItem, qiIndexSqlBusiness);
                        qiIndexSqlBusiness.setBusinessNa(business.getBusinessNa());
                        qiIndexSqlBusiness.setBusinessValue(business.getBusinessValue());
                        qiIndexSqlBusiness.setModuleType(business.getModuleType());
                        LambdaQueryWrapper<QiIndexSqlBusiness> lambdaQueryWrappers = new LambdaQueryWrapper<>();
                        lambdaQueryWrappers.eq(QiIndexSqlBusiness::getIndexCode,qiIndexItem.getIndexCode())
                                .eq(QiIndexSqlBusiness::getItemCode,qiIndexItem.getItemCode())
                                .eq(QiIndexSqlBusiness::getMajorCode,qiIndexItem.getMajorCode())
                                .eq(QiIndexSqlBusiness::getModuleType,business.getModuleType())
                                .eq(QiIndexSqlBusiness::getBusinessValue,business.getBusinessValue())
                                .eq(QiIndexSqlBusiness::getDelFlag,0);
                        qiIndexSqlBusinessService.saveOrUpdate(qiIndexSqlBusiness, lambdaQueryWrappers);
                    }
                }

            }
        }
        return Result.succ(1,"同步成功",null);
    }
}
